package net.system.sys.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.core.base.dao.CommonDAO;
import net.core.common.StrUtils;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ConfigServiceImpl implements IConfigService {

	private static final Log logger = LogFactory.getLog(ConfigServiceImpl.class);
	@Autowired
	private CommonDAO commonDAO;
	@Override
	public List<HashMap> getWWWCfg() {
		String sql = "select sc_name, sc_value,sc_desc,rec_id from s_sysconfig where sc_group ='WWWCFG'";
		List<HashMap> map = commonDAO.getDataList(sql);
		return map;
	}
	@Override
	public void saveWWWCFG(HashMap reqMap) {
		String[] aSaveCfg={"WWW_NAME","WWW_DOMAIN","WWW_LOGO","WWW_KEYWORD","WWW_DESC","WWW_STATICCODE"};
		List<String> list = new ArrayList<String>();
		String sql = "";
		for(int i=0;i<aSaveCfg.length;i++){
			sql = "update s_sysconfig set sc_value='"+StrUtils.getNotNullStrValue(reqMap.get(aSaveCfg[i]))+"' where sc_name='"+aSaveCfg[i]+"' and sc_group ='WWWCFG'";
			list.add(sql);
		}
		System.out.println(list.toString());
		logger.debug(list.toString());
		commonDAO.batchUpdate(list, logger);
	}
	@Override
	public List<HashMap> getConfigByFroup(HashMap reqMap) {
		String group = StrUtils.getNotNullStrValue(reqMap.get("group"));
		String sql = "select sc_name, sc_value,sc_desc,rec_id from s_sysconfig where sc_group ='"+group+"'";
		List<HashMap> map = commonDAO.getDataList(sql);
		return map;
	}
	@Override
	public void saveConfig(HashMap reqMap) {
		String group = StrUtils.getNotNullStrValue(reqMap.get("group"));
		List<HashMap> listConfig = getConfigByFroup(reqMap);
		
		List<String> list = new ArrayList<String>();
		String sql = "";
		if(null!=listConfig && !listConfig.isEmpty()){
			int count= listConfig.size();
		for(int i=0;i<count;i++){
			HashMap map = listConfig.get(i);
			String name = StrUtils.getNotNullStrValue(map.get("sc_name"));
			sql = "update s_sysconfig set sc_value='"+StrUtils.getNotNullStrValue(reqMap.get(name))+"' where sc_name='"+name+"' and sc_group ='"+group+"'";
			list.add(sql);
		}
		}
		System.out.println(list.toString());
		logger.debug(list.toString());
		commonDAO.batchUpdate(list, logger);
		
	}

}
